package com.xiuxia.bopicturebackend.controller;

import com.xiuxia.bopicturebackend.common.BaseResponse;
import com.xiuxia.bopicturebackend.common.ResultUtils;
import com.xiuxia.bopicturebackend.exception.ErrorCode;
import com.xiuxia.bopicturebackend.exception.ThrowUtils;
import com.xiuxia.bopicturebackend.model.dto.space.analyze.*;
import com.xiuxia.bopicturebackend.model.entity.Space;
import com.xiuxia.bopicturebackend.model.entity.User;
import com.xiuxia.bopicturebackend.model.vo.space.analyze.*;
import com.xiuxia.bopicturebackend.service.SpaceAnalyzeService;
import com.xiuxia.bopicturebackend.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName SpaceAnalyzeController
 * @Author CHENBO
 * @Date 2025/7/17 9:46
 */
@RestController
@RequestMapping("/space/analyze")
public class SpaceAnalyzeController {
    @Resource
    private UserService userService;
    @Resource
    private SpaceAnalyzeService spaceAnalyzeService;

    /**
     * 获取空间使用情况
     *
     * @param spaceUsageAnalyzeRequestDTO 请求体
     * @param request                     请求
     * @return 响应
     */
    @PostMapping("/usage")
    public BaseResponse<SpaceUsageAnalyzeResponseVO> getSpaceUsageAnalyze(@RequestBody SpaceUsageAnalyzeRequestDTO spaceUsageAnalyzeRequestDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUsageAnalyzeRequestDTO == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        SpaceUsageAnalyzeResponseVO spaceUsageAnalyze = spaceAnalyzeService.getSpaceUsageAnalyze(spaceUsageAnalyzeRequestDTO, loginUser);
        return ResultUtils.success(spaceUsageAnalyze);
    }

    /**
     * 获取空间图片分类情况
     *
     * @param spaceCategoryAnalyzeRequestDTO 请求体
     * @param request                        请求
     * @return 响应
     */
    @PostMapping("/category")
    public BaseResponse<List<SpaceCategoryAnalyzeResponseVO>> getSpaceCategoryAnalyze(@RequestBody SpaceCategoryAnalyzeRequestDTO spaceCategoryAnalyzeRequestDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceCategoryAnalyzeRequestDTO == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceCategoryAnalyzeResponseVO> spaceCategoryAnalyze = spaceAnalyzeService.getSpaceCategoryAnalyze(spaceCategoryAnalyzeRequestDTO, loginUser);
        return ResultUtils.success(spaceCategoryAnalyze);
    }

    /**
     * 获取空间图片标签情况
     *
     * @param spaceTagAnalyzeRequestDTO 请求体
     * @param request                   请求
     * @return 响应
     */
    @PostMapping("/tag")
    public BaseResponse<List<SpaceTagAnalyzeResponseVO>> getSpaceTagAnalyze(@RequestBody SpaceTagAnalyzeRequestDTO spaceTagAnalyzeRequestDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceTagAnalyzeRequestDTO == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceTagAnalyzeResponseVO> spaceTagAnalyze = spaceAnalyzeService.getSpaceTagAnalyze(spaceTagAnalyzeRequestDTO, loginUser);
        return ResultUtils.success(spaceTagAnalyze);
    }

    /**
     * 获取空间图片大小情况
     *
     * @param spaceSizeAnalyzeRequestDTO 请求体
     * @param request                    请求
     * @return 响应
     */
    @PostMapping("/size")
    public BaseResponse<List<SpaceSizeAnalyzeResponseVO>> getSpaceSizeAnalyze(@RequestBody SpaceSizeAnalyzeRequestDTO spaceSizeAnalyzeRequestDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceSizeAnalyzeRequestDTO == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceSizeAnalyzeResponseVO> spaceSizeAnalyze = spaceAnalyzeService.getSpaceSizeAnalyze(spaceSizeAnalyzeRequestDTO, loginUser);
        return ResultUtils.success(spaceSizeAnalyze);
    }

    /**
     * 获取空间用户上传图片行为情况
     *
     * @param spaceUserAnalyzeRequestDTO 请求体
     * @param request                    请求
     * @return 响应
     */
    @PostMapping("/user")
    public BaseResponse<List<SpaceUserAnalyzeResponseVO>> getSpaceUserAnalyze(@RequestBody SpaceUserAnalyzeRequestDTO spaceUserAnalyzeRequestDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUserAnalyzeRequestDTO == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceUserAnalyzeResponseVO> spaceUserAnalyze = spaceAnalyzeService.getSpaceUserAnalyze(spaceUserAnalyzeRequestDTO, loginUser);
        return ResultUtils.success(spaceUserAnalyze);
    }

    /**
     * 获取空间排行榜
     *
     * @param spaceRankAnalyzeRequest 请求体
     * @param request                 请求
     * @return 响应
     */
    @PostMapping("/rank")
    public BaseResponse<List<Space>> getSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequestDTO spaceRankAnalyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceRankAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<Space> resultList = spaceAnalyzeService.getSpaceRankAnalyze(spaceRankAnalyzeRequest, loginUser);
        return ResultUtils.success(resultList);
    }

}
